<?php

namespace backend\models\service;

use Yii;
use yii\data\Sort;
use backend\models\odm\NovelChapterOdm;

class Chapter extends NovelChapterOdm {
    
    private static $_items = [];
    
    public static function items($id) {
        if (!isset(self::$_items[$id])) {
            self::loadItems($id);
        }
        return self::$_items[$id];
    }

    private static function loadItems($id) {
        self::$_items[$id] = [];
        $models = static::find()->where(['chapter_novel_id' => $id])->orderBy(['chapter_order' => SORT_ASC])->asArray()->all();
        
        foreach ($models as $model) {
            self::$_items[$id][$model['chapter_id']] = $model['chapter_name'];
        }
    }
    
    
    public function getChapters($novel_id){
        $sql = "select chapter_id, chapter_name from xs_novel_chapter where  chapter_novel_id = " .$novel_id;
        $res = Yii::$app->db->createCommand($sql)->queryAll();
        return $res;
    }
    
     public function getChaptersInfo($id){
        $sql = "select chapter_id, chapter_name from xs_novel_chapter where  chapter_id = " .$id;
        $res = Yii::$app->db->createCommand($sql)->queryOne();
        return $res;
    }

}
